*open the data file rep_data.dta
*see file readme.pdf



***Define samples:

**OT 39--Discussed by CJ?
firthlogit cjdisc u_usp1  u_alconflict u_acconflict ///
u_dissent c_intrev u_issuedummy  hasac ///
 comp_ideo if aot==39 & u_ifp ==0 & u_ca==1, nolog
estimates store m39comp1


**basic regression (Warren Discuss List):
logit cv68_cjdisc cv68_u_usp1 cv68_u_alconflict cv68_u_acconflict   ///
cv68_u_dissent cv68_c_intrev  cv68_u_issuedummy cv68_hasac ///
cv68_comp_ideo if cv68_cert !=., nolog
est store m68comp1


*82 CJ DL?
logit cv82_cjdisc cv82_u_usp1  cv82_u_alconflict cv82_u_acconflict ///
cv82_u_dissent cv82_c_intrev cv82_u_issuedummy  cv82_hasac ///
cv82_comp_ideo if cv82_cert !=. & cv82_tagdocky==1, nolog
estimates store m82comp1
*note that the OT 82 data is justice-level, so we condition on 
*cv82_tagdocky, which selects one observation for each docket.


est restore m39comp1
gen sm39comp1=e(sample)

est restore m68comp1
gen sm68comp1=e(sample)

est restore m82comp1
gen sm82comp1=e(sample)






*create covariates from year-specific variables:  
local ifcd if sm39comp1 | sm68comp1 | sm82comp1

gen __cjdl=min(cjdisc,cv68_cjdisc,cv82_cjdisc) `ifcd'
gen __usp1=min(u_usp1,cv68_u_usp1,cv82_u_usp1) `ifcd' 
gen __ir=min(c_intrev,cv68_c_intrev,cv82_c_intrev) `ifcd'
gen __alc=min(u_alconflict,cv68_u_alconflict,cv82_u_alconflict) `ifcd'
gen __acc=min(u_acconflict,cv68_u_acconflict,cv82_u_acconflict) `ifcd'
gen __diss=min(u_dissent,cv68_u_dissent,cv82_u_dissent) `ifcd'
gen __iss=min(u_issuedummy,cv68_u_issuedummy,cv82_u_issuedummy) `ifcd'
gen __ci=min(comp_ideo,cv68_comp_ideo,cv82_comp_ideo) `ifcd'
gen __ab=min(hasac,cv68_hasac,cv82_hasac) `ifcd'


gen __uterm=39 if aot==39
replace __uterm=68 if czobs
replace __uterm=82 if cwobs

*create group variables for regressions/sdcasepick
gen __gr3968=0 if aot==39
replace __gr3968=1 if czobs

gen __gr3982=0 if aot==39
replace  __gr3982=1 if cwobs

gen __gr6882=0 if czobs
replace __gr6882=1 if cwobs


**descriptives: relative frequency of covariates, by term.** (Table 1)
foreach var of varlist  __usp1 __ir __alc __acc __diss __iss __ci __ab{
bysort __uterm: summ `var' if sm39comp1 | sm68comp1 | sm82comp1,det
}

**descriptives: frequencies.**
foreach var of varlist  __usp1 __ir __alc __acc __diss __iss __ci __ab{
bysort __uterm: tab __cjdl `var' if sm39comp1 | sm68comp1 | sm82comp1
}


*********************39 ("Group 0") / 68 ("Group 1")****************************
gen distance=. //to store descriptives of distance 
local di=1 //same

*Regression (not presented in paper) on which Table 2 results are based:
firthlogit __cjdl __gr3968##(__usp1 __ir __alc __acc __diss __iss __ci __ab) ///
if  sm68comp1 | sm39comp1 

tempvar p
predict `p' if e(sample)
cap gen pr3968= (exp(`p'))/(1+exp(`p'))


*Partial Table 2 Results (see readme.pdf for interpretation):
local vl __usp1 __ir __alc __acc __diss __iss __ci __ab

foreach v of local vl{
sdcasepick `v', prv(pr3968) prlb(.45) prub(.55) gr(__gr3968) ///
expression(exp(predict(xb))/(1+exp(predict(xb)))) sec
replace distance=`r(dist)' in `di'
local di=`di'+1
}



*********************39 ("Group 0") / 82 ("Group 1")****************************

*Regression (not presented in paper) on which Table 2 results are based:
firthlogit __cjdl __gr3982##(__usp1 __ir __alc __acc __diss __iss __ci __ab) ///
if sm39comp1 | sm82comp1 
tempvar p
predict `p' if e(sample)
cap gen pr3982= (exp(`p'))/(1+exp(`p'))

*Partial Table 2 Results (see readme.pdf for interpretation):
local vl __usp1 __ir __alc __acc __diss __iss __ci __ab
foreach v of local vl{
sdcasepick `v', prv(pr3982) prlb(.45) prub(.55) gr(__gr3982) ///
expression(exp(predict(xb))/(1+exp(predict(xb)))) sec
replace distance=`r(dist)' in `di'
local di=`di'+1
}



*********************68 ("Group 0") / 82 ("Group 1")****************************

*Regression (not presented in paper) on which Table 2 results are based:
logit __cjdl __gr6882##(__usp1 __ir __alc __acc __diss __iss __ci __ab) ///
if sm68comp1 | sm82comp1 
cap predict pr6882

*Partial Table 2 Results (see readme.pdf for interpretation):
local vl __usp1 __ir __alc __acc __diss __iss __ci __ab
foreach v of local vl{
sdcasepick `v', prv(pr6882) prlb(.45) prub(.55) gr(__gr6882) sec
replace distance=`r(dist)' in `di'
local di=`di'+1
}

summ distance, detail //descriptives for distance
drop distance
